package asher.dotatbg.http.configs;

import java.io.File;
import java.util.Date;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import asher.dotatbg.logfactory.LogFactory;

/**
 * 总配置文件的属性携带类，其构造函数完成从配置文件中 读取 属性到单例化的实例中来的工作
 * @author AsherYuan
 */
public class MainConfig {
	
	private static Logger logger = LogFactory.getRootLogger();
	
	private static MainConfig instance = null;
	
	/**
	 * 一下为对应配置文件的属性
	 */
	public String defalutResponserPackage;
	
	
	private MainConfig() {
		String classpath = System.getProperty("java.class.path");
		String[] all = classpath.split(";");
	    File f = new File(all[0] + "/mainconfig.xml");
	    if(!f.exists()) {
	    	logger.info("错误！配置文件 mainconfig.xml 找不到！请检查路径." + new Date().toString());
	    	System.exit(1);
	    }
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
			doc = reader.read(f);
		} catch (DocumentException e) {
			logger.info("错误！配置文件 mainconfig.xml 读取出错！请检查文件" + new Date().toString());
		}
        Element root = doc.getRootElement();
        Element data;
        Iterator<?> itr = root.elementIterator("value");
        data = (Element) itr.next();
        this.defalutResponserPackage = data.elementText("defalutResponserPackage").trim();
	}

	public static MainConfig getInstance() {
		if(instance == null) {
			instance = new MainConfig();
		}
		return instance;
	}
	
}
